package com.xiaoma.tpo.service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaoma.tpo.dao.TestDao;
import com.xiaoma.tpo.dao.UserDao;
import com.xiaoma.tpo.domain.Test;
import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.utils.TPOUtils;

@Service
public class TestService {

    @Autowired
    private TestDao testDao;
    
    @Autowired
    private UserDao userDao;
    @Transactional
    public void insert(Test test){//事务控制例子
        testDao.insert(test);
        testDao.insert1();
        int a = 1/0;
    }
    
    public void updateUser() {
        List<User> users = userDao.findAll();
        Date now = new Date();
        
        for (User user : users) {
            user.setLastVisit(TPOUtils.getMaxDate());
            user.setToken(UUID.randomUUID().toString());
            user.setCreatedAt(now);
            user.setPassword("99999999");
            userDao.update(user);
        }
    }
    
    public Test findById(int id) {
        return testDao.getById(id);
    }
    
    public List<Test> findU(int id) {
        return testDao.findU(id);
    }
}
